package com.nega.commons.spider.core;

import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import com.nega.modules.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: Garcia
 * @CreateDate: 2019/05/31 19:02
 * @Description: IP池，自动补充IP，IP多次使用，当无效后剔除
 */
@Slf4j
public class IPSourceMoreTime implements IPSourcePool {

    private ReentrantLock mLock = new ReentrantLock();

    private List<String> pool = new ArrayList<>();

    @Resource
    private SpiderCoreService spiderCoreService;

    @Resource
    private ConfigService configService;

    private String IP_URL;

    private String CHECK_IP_URL;

    @Scheduled(fixedRate = 1000)
    @Async
    public void initIp(){
        if (pool.size()<1){
            log.info("复用IP池当前数量:{}，IP池数量较低，开始补充>>>>>>",pool.size());
            try {
                if (StringUtils.isBlank(IP_URL)){
                    String ipUrl = configService.getProperty("ipUrl");
                    IP_URL=ipUrl;
                }
                Document document = Jsoup.connect(IP_URL).get();
                String ips = document.text();
                if (StringUtils.isNotBlank(ips)){
                    if (ips.contains("{")){
                        pool.add("FAIL");
                        return;
                    }
                    pool.add(ips.trim());
                }else{
                    pool.add("FAIL");
                }
                log.info("复用IP池补充完成，当前数量:{}",pool.size());
            } catch (IOException e) {
                log.info("复用IP池补充异常",e);
                pool.add("FAIL");
            }
        }
    }

    private void removeIp(String ip){
        if (mLock.tryLock()){
            if (pool.size()>0){
                pool.remove(ip);
            }
            mLock.unlock();
        }
    }

    @Override
    public String getIp(){
        String ip;
        while (true){
            if (pool.size()>0){
                ip = pool.get(0);
                if ("FAIL".equals(ip)){
                    removeIp(ip);
                    break;
                }
                if (StringUtils.isBlank(CHECK_IP_URL)){
                    String checkIpUrl = configService.getProperty("checkIpUrl");
                    CHECK_IP_URL=checkIpUrl;
                }
                Document document = spiderCoreService.getDocument(CHECK_IP_URL,null,ip);
                if (document==null){
                    removeIp(ip);
                    continue;
                }
                if ("<head>".contains(document.head().tagName())){
                    break;
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                log.error("复用IP池，线程睡眠异常");
            }
        }
        return ip;
    }
}
